package com.kichina.parking.upstream;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.kichina.parking.exception.extend.JsonParseException;
import com.kichina.parking.service.logic.bean.ChargeRecord;
import com.kichina.parking.service.logic.bean.StationaryCarRecord;
import com.kichina.parking.service.logic.bean.WhitelistInfoRecord;
import com.kichina.parking.service.logic.service.ChargeRecordService;
import com.kichina.parking.service.logic.service.StationaryCarRecordService;
import com.kichina.parking.service.logic.service.WhitelistInfoRecordService;
import com.kichina.parking.utils.JsonToEntityUtil;
import com.kichina.parking.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Surging
 * @version 1.0
 * @ClassName MonthPayUpstreamHandler
 * @date 2022-09-14 20:03
 * @desc 车场白名单业务处理(停车管理上报白名单相当于APP下发白名单)
 */
@Component
@Slf4j
public class Parking8WhiteListUpstreamHandler {

    private final WhitelistInfoRecordService whitelistInfoRecordService;

    private final ChargeRecordService chargeRecordService;

    private final StationaryCarRecordService stationaryCarRecordService;

    public Parking8WhiteListUpstreamHandler(WhitelistInfoRecordService whitelistInfoRecordService, ChargeRecordService chargeRecordService, StationaryCarRecordService stationaryCarRecordService) {
        this.whitelistInfoRecordService = whitelistInfoRecordService;
        this.chargeRecordService = chargeRecordService;
        this.stationaryCarRecordService = stationaryCarRecordService;
    }

    /**
     * @param jsonObject 数据实体
     * @author Surging
     * @Description 白名单变化后上传信息到服务端；
     * 用户注册接口。
     * @Return void
     * @date 20:53 2022-09-15
     **/
    public void recordParkingWhiteListUpstream(JSONObject jsonObject) throws JsonParseException {
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        WhitelistInfoRecord whitelistInfoRecord;
        try {
            whitelistInfoRecord = JsonToEntityUtil.jsonToEntity(jsonObject, WhitelistInfoRecord.class);
            LambdaQueryWrapper<WhitelistInfoRecord> eq = new QueryWrapper<WhitelistInfoRecord>().lambda()
                    .eq(WhitelistInfoRecord::getParkid, whitelistInfoRecord.getParkid())
                    .eq(WhitelistInfoRecord::getCardId, whitelistInfoRecord.getCardId());
            if(3==whitelistInfoRecord.getOperateType()){
                //删除白名单车牌信息
                whitelistInfoRecordService.remove(eq);
                return;
            }
            if (Objects.nonNull(jsonObject.getString("startdate"))){
                whitelistInfoRecord.setStartdate(jsonObject.getString("startdate"));
                //字符串格式两种 2022-10-15; 2022-09-16 15:54:00;
                if(String.valueOf(jsonObject.getString("startdate")).length()<15){
                    whitelistInfoRecord.setStartdateDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("startdate"))));
                } else {
                    whitelistInfoRecord.setStartdateDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("startdate"))));
                }
            }
            if (Objects.nonNull(jsonObject.getString("validdate"))) {
                whitelistInfoRecord.setValiddate(jsonObject.getString("validdate"));
                if(String.valueOf(jsonObject.getString("validdate")).length()<15){
                    whitelistInfoRecord.setValiddateDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("validdate"))));
                }else {
                    whitelistInfoRecord.setValiddateDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("validdate"))));
                }
            }
            if (Objects.nonNull(jsonObject.getString("create_time"))) {
                whitelistInfoRecord.setCreateTime(jsonObject.getString("create_time"));
                if(String.valueOf(jsonObject.getString("create_time")).length()<15){
                    whitelistInfoRecord.setCreateTimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("create_time"))));
                }else {
                    whitelistInfoRecord.setCreateTimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("create_time"))));
                }
            }
            if (Objects.nonNull(jsonObject.getString("modify_time"))) {
                whitelistInfoRecord.setModifyTime(jsonObject.getString("modify_time"));
                if(String.valueOf(jsonObject.getString("modify_time")).length()<15){
                    whitelistInfoRecord.setModifyTimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("modify_time"))));
                } else {
                    whitelistInfoRecord.setModifyTimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("modify_time"))));
                }

            }
            if(1==whitelistInfoRecord.getOperateType()){
                //保存添加人员信息
                whitelistInfoRecord.setOperaterAdd(whitelistInfoRecord.getOperator());
            }
            List<WhitelistInfoRecord> whitelistInfoRecords = whitelistInfoRecordService.list(eq);
            Date  date = simpleDateFormat.parse("1970-01-01 08:00:00");
            if (whitelistInfoRecord.getModifyTimeDate().before(date)){
                whitelistInfoRecord.setModifyTimeDate(new Date());
            }
            if (CollectionUtils.isNotEmpty(whitelistInfoRecords)){
                whitelistInfoRecord.setDealTag(true);
                //更新
                whitelistInfoRecord.setUpdateTime(new Date());
                whitelistInfoRecordService.update(whitelistInfoRecord, eq);
            }else {
                //添加白名单车信息
                whitelistInfoRecord.setDealTag(true);
                whitelistInfoRecord.setWhitelistInfoRecordId(UUIDUtil.getUUID());
                whitelistInfoRecord.setUpdateTime(new Date());
                //原实体有createTime字段，serviceCreateTime作区分
                whitelistInfoRecord.setServiceCreateTime(new Date());
                whitelistInfoRecordService.save(whitelistInfoRecord);

            }
            //添加固定车记录信息
            List<StationaryCarRecord> stationaryCarRecordList = stationaryCarRecordService.list(new QueryWrapper<StationaryCarRecord>().lambda()
                    .eq(StationaryCarRecord::getCardId, whitelistInfoRecord.getCardId()));
            if (CollectionUtils.isEmpty(stationaryCarRecordList)){
                StationaryCarRecord stationaryCarRecord = new StationaryCarRecord();
                stationaryCarRecord.setStationaryCarId(UUIDUtil.getUUID());
                stationaryCarRecord.setCardId(whitelistInfoRecord.getCardId());
                stationaryCarRecord.setCarNumber(whitelistInfoRecord.getCarNumber());
                stationaryCarRecord.setMessage("注册固定车成功(白名单上报添加)");
                stationaryCarRecord.setParkid(whitelistInfoRecord.getParkid());
                stationaryCarRecord.setResultCode(0);
                stationaryCarRecord.setService(whitelistInfoRecord.getService());
                stationaryCarRecord.setCreateTime(new Date());
                stationaryCarRecord.setUpdateTime(new Date());
                stationaryCarRecordService.save(stationaryCarRecord);
            }

        } catch (ParseException e) {
            log.error("数据格式不合法，解析失败");
            throw new JsonParseException("数据格式不合法，解析失败");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
